$(function(){
	var dictcode = getUrlParam('dictcode');
	initForm(dictcode);
	getDictDetal(dictcode);				//绑定明细数据
	
	//明细数据的查询功能
	$('#keyword').bind('input propertychange', function() {
		var val = $(this).val();
		
		$('#tableDictDetail').find('tbody').find('tr').each(function(){
			var isexist = false;
			
			$(this).find('[type="text"]').each(function(){
				var objval = $(this).val();
				
				if(objval.indexOf(val) >= 0){
					isexist = true;
					return false;
				}
			})
			
			
			if(isexist)
				$(this).show();
			else 
				$(this).hide();
			
//			var obj = $(this).find('[name="itemname"]');
//			var objval = obj.val();
//			
//			if(objval.indexOf(val) == -1)
//				$(this).hide();
//			else 
//				$(this).show();
		})
	});  
})

function initForm(dictcode){

	var infoUrl = projectPath + "dict/getInfo.do";
	
	$.ajax({ 
		url: infoUrl,
		data: {"dictcode": dictcode},
		dataType: "json",
		success: function(data){
			if(data.result == 1){   
				bindFormDictInfo(data.datas);
			}
      	}
	});
}

function bindFormDictInfo(data){
	$('[name="dictcode"]').val(data.dictcode);
	$('[name="dictname"]').val(data.dictname);
	
	var ifallow = data.ifallow;
	if(ifallow == "1"){
		$('[name="ifallow"]').attr("checked",true); 
	}
	else{
		$('[name="ifallow"]').attr("checked",false); 
	}
	
}


//查询当前字典的所有明细记录
function getDictDetal(dictcode){

	var infoUrl = projectPath + "dict/getDictDetal.do";
	
	$.ajax({ 
		url: infoUrl,
		data: {"dictcode": dictcode},
		dataType: "json",
		success: function(data){
			if(data.result == 1){   
				bindFormDictDetailList(data.datas);
			}
      	}
	});
}


function bindFormDictDetailList(data){
	for (var i = 0; i < data.length; i++) {
		bindDetailList(data[i]);
	}
}


function save(){
	var dictData = $("#dictForm").serializeObject();
	if(dictData.ifallow == undefined){
		dictData['ifallow'] = 0;
	}

	var dictDetailData = $('#dictDetailForm').serializeObjectArray();
	
	
	var dictDetail = {};
	dictDetail['dictDetail'] = 'dictDetail';
	dictDetail['row'] = dictDetailData;
	
	dictData['table'] = dictDetail
	

	if(!check(dictData))
		return true;
	
	$.ajax({ 
		method: 'post',
		url: basePath() + 'dict/update.do',
		data: JSON.stringify(dictData),
		dataType: "json",
        contentType: "application/json",
		success: function(data){	
	    	window.wxc.xcConfirm(data.info, window.wxc.xcConfirm.typeEnum.success, {
	    		onOk: function(){
	    			if(data.result == 1){
	    				parent.refreshData();		//刷新数据
	    			} 
	    		}
	    	});
      	}
	});
}

function check(data){	
	var result = true;
	

	if($('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').length == 0){
		window.wxc.xcConfirm('请增加<span style="color:red;"><strong>字典明细</strong></span>的行数据！', window.wxc.xcConfirm.typeEnum.info);
		return false;
	}
	
	
	var mustStr = "dictcode,dictname";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("placeholder") + "！<br />";
			
			result = false;
		}
	}
	

	
	
	if(strMsg != ""){
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);
	}
	
	return result;
}
